package com.example.dqhl.recycleviewdemo.service;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Created by dell on 2017/10/31.
 */

public class BluetoothReceiver extends BroadcastReceiver {

    private BluetoothInterface bluetoothInterface;

    public BluetoothReceiver(BluetoothInterface udpAcceptInterface) {
        this.bluetoothInterface = udpAcceptInterface;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothDevice.ACTION_FOUND)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                bluetoothInterface.success(device);
            } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                bluetoothInterface.faile();
            }
        }
    }

    public interface BluetoothInterface {
        void success(BluetoothDevice device);

        void faile();
    }
}
